/**
 * 
 */
package com.myBase.core.facade.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.myBase.core.dao.GoldcoinActivitycashpoolDao;
import com.myBase.core.entity.GoldcoinActivitycashpool;
import com.myBase.core.exception.PlatformException;
import com.myBase.core.facade.service.EBaiGoldCoinInPutService;
import com.myBase.core.facade.service.GoldcoinActivitycashpoolServiceFacade;
import com.myBase.common.persistence.Page;
import com.myBase.common.service.CrudAbstractService;

/**
 * 活动资金池表Service
 * @author 活动资金池表
 * @version 2017-05-04
 */
@Service("goldcoinActivitycashpoolServiceFacade")
@Transactional(readOnly = true)
public class GoldcoinActivitycashpoolServiceFacadeImpl
        extends CrudAbstractService<GoldcoinActivitycashpoolDao, GoldcoinActivitycashpool>
        implements GoldcoinActivitycashpoolServiceFacade {

    @Autowired
    EBaiGoldCoinInPutService goldCoinInputService;

    @Autowired
    GoldcoinActivitycashpoolDao goldcoinActivitycashpoolDao;

    public GoldcoinActivitycashpool get(String id) {
        return super.get(id);
    }

    public List<GoldcoinActivitycashpool> findList(GoldcoinActivitycashpool goldcoinActivitycashpool) {
        return super.findList(goldcoinActivitycashpool);
    }

    public Page<GoldcoinActivitycashpool> findPage(Page<GoldcoinActivitycashpool> page,
            GoldcoinActivitycashpool goldcoinActivitycashpool) {
        return super.findPage(page, goldcoinActivitycashpool);
    }

    @Transactional(readOnly = false)
    public void save(GoldcoinActivitycashpool goldcoinActivitycashpool) {
        super.save(goldcoinActivitycashpool);
    }

    @Transactional(readOnly = false)
    public void delete(GoldcoinActivitycashpool goldcoinActivitycashpool) {
        super.delete(goldcoinActivitycashpool);
    }

    /**
     * * getALLGoldActivity(获取所有活动)
     * @param  
     * @return List<HashMap<String,Object>>  
     * @Exception 
     */
    public List<HashMap<String, Object>> getALLGoldActivity(String branchid) throws Exception {
        logger.info("查询所有活动 service 开始,branchid=" + branchid);
        List<HashMap<String, Object>> allGoldActivity = goldcoinActivitycashpoolDao.getALLGoldActivity(branchid);
        logger.info("查询所有活动 service 结束");
        return allGoldActivity;
    };

    // 每一个金币活动进行结算
    public void updateSettlementData(BigDecimal leftAmount, BigDecimal totalAmount, Integer activityId) {
        goldcoinActivitycashpoolDao.updateSettlement(leftAmount, totalAmount, activityId);
    }

    @Override
    public GoldcoinActivitycashpool getBeanByActivityAndBranchId(String activityId, String branchId) {
        List<GoldcoinActivitycashpool> list = (List<GoldcoinActivitycashpool>) goldcoinActivitycashpoolDao
                .getBeanByActivityAndBranchId(activityId, branchId);

        //默认前台结果为null
        GoldcoinActivitycashpool goldcoinActivitycashpool = null;
        //如果搜索结果不为空，就获得唯一的一条数据及第一条数据
        if (list.size() > 0) {
            goldcoinActivitycashpool = list.get(0);
        }

        return goldcoinActivitycashpool;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, rollbackFor = Exception.class)
    public void saveOrUpdateBySelf(GoldcoinActivitycashpool goldcoinActivitycashpool) throws PlatformException {
        //根据是否是新数据标注位
//        if (goldcoinActivitycashpool.getIsNewRecord()) {
            //新插入一条数据
//            goldcoinActivitycashpool.preInsert();
            int goldcoinActivitycashpoolNum = goldcoinActivitycashpoolDao.insert(goldcoinActivitycashpool);
            if (goldcoinActivitycashpoolNum != 1) {
                throw new PlatformException("新增ebei入金失败");
            }

//        } else {
//            //update数据
////            goldcoinActivitycashpool.preUpdate();
//            int goldcoinActivitycashpoolNum = goldcoinActivitycashpoolDao.updateBySelf(goldcoinActivitycashpool);
//            if (goldcoinActivitycashpoolNum != 1) {
//                throw new PlatformException("更新ebei入金失败");
//            }
//        }

    }

    @Override
    public HashMap<String, Object> getGoldCoinActivitycashpoolCanUseCash(Integer activityId) {
        HashMap<String, Object> map = goldcoinActivitycashpoolDao.getCanUseCash(activityId);
        if (map == null) {
            map = new HashMap<String, Object>();
        }
        return map;
    }

}